/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;

import org.hibernate.Session;
import org.hibernate.Transaction;
import javax.validation.ValidationException;

import tcc.sglj.dao.*;
import tcc.sglj.model.*;
import tcc.sglj.util.*;
import org.hibernate.*;

/**
 *
 * @author Leandro
 */
@WebServlet(name = "Teste", urlPatterns = {"/teste.js"})
public class Teste extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();


        /*
        User user = new User();
        user.setCpf("3423423");
        user.setEmail("dfasdfas");*/
        try {

            Session session = HibernateUtil.getSession();
            Transaction tx = session.beginTransaction();
           
            /*User user = new User();
            user.setEmail("jhonatan@gmail.com");
            user.setName("Jhonatan luiz");
            user.setPassword("123456");
            user.setSign(new Date());
            user.setCpf("12345678");
            user.setStatus(true);*/
           
            tx.commit();

            out.println("salvo!!!");



        } catch (ValidationException e) {
            out.println(e.getLocalizedMessage());
        }
        /* Perfil perfil = new Perfil();
        perfil.setPerfil("Cliente");
        session.save(perfil);
        tx.commit();*/

        /* try {
        Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();
        
        User user = new User();
        user.setCpf("00740456105");
        user.setEmail("leandro@gmail.com");
        user.setName("leandro o grande");
        user.setPassword("1234567890");
        user.setSign(new Date());
        user.setStatus(true);
        
        session.save(user);
        
        Address address = new Address();
        address.setAddressUser("Setor de Mansões Mestre darmas 1 modulo b casa 17");
        address.setCity("Planaltina");
        address.setComplement("Setor residencial");
        address.setNumber(17);
        address.setDistrict("estancia");
        address.setZipCode("73380");
        address.setState(DaoState.getTypeCityById(1));
        
        user.setAddress(address);
        session.save(address);
        
        List<Phone> meuPhone = new ArrayList<Phone>();
        Phone phone = new Phone();
        phone.setDDD(61);
        phone.setNumberPhone("93576547");
        phone.setStatus(false);
        phone.setTypePhone(DaoPhone.getTypePhoneById(1));
        phone.setIdPhone(1);
        phone.setUserPhone(user);
        session.save(phone);
        
        
        meuPhone.add(phone);
        user.setPhoneUser(meuPhone);
        
        
        
        
        PaymentType paymentType = new PaymentType();
        paymentType.setTpPay("Dinheiro");
        paymentType.setIdPay(1);
        session.save(paymentType);
        
        Price price = new Price();
        price.setDtPrice(new Date());
        price.setNuPrice(22.34);
        price.setStatus(Boolean.TRUE);
        session.save(price);
        
        
        ServiceType serviceType = new ServiceType();
        serviceType.setStatus(Boolean.TRUE);
        serviceType.setTpServico("lavagem a seco");
        serviceType.setPrice(price);
        session.save(serviceType);
        
        /*ProductType productType = new ProductType();
        productType.setTpServico("Lavagem a seco");
        productType.setStatus(false);
        productType.setPrice(price);
        session.save(price);
        session.save(productType);
        
        
        
        EmployeeRole employeeRole = new EmployeeRole();
        employeeRole.setPerfil("funcionario");
        session.save(employeeRole);
        
        Employee employee = new Employee();
        employee.setName("leandro o grande");
        employee.setEmail("leandro@gmail.com");
        employee.setCpf("00740456105");
        employee.setCtps("trabalhador");
        employee.setPisPasep("12343445546");
        employee.setRole(employeeRole);
        employee.setPassword("1234567890");
        employee.setSign(new Date());
        employee.setAddress(address);
        session.save(employee);
        
        ServiceOrder serviceOrder = new ServiceOrder();
        serviceOrder.setDtData(new Date());
        serviceOrder.setObservation("testetetstetststet");
        serviceOrder.setPaymentType(paymentType);
        serviceOrder.setEmployee(employee);
        serviceOrder.setProductType(serviceType);
        serviceOrder.setUser(user);
        serviceOrder.setPayment(true);
        session.save(serviceOrder);
        
        
        
        /* Product product = new Product();
        product.setManufacturer("Minuano");
        product.setProduct("tinta para pneu");
        product.setStatus(true);
        session.save(product);
        MarkVehicle markVehicle = new MarkVehicle();
        markVehicle.setNameMark("ford");
        //markVehicle.setVehicleClient(myvehicle);
        session.save(markVehicle);
        
        
        
        
        
        List<VehicleClient> myvehicle = new ArrayList<VehicleClient>();
        VehicleClient vehicleClient = new VehicleClient();
        vehicleClient.setColor("Preto");
        vehicleClient.setVehicleBoard("jkpl2324");
        vehicleClient.setVehicleModel("fiesta");
        vehicleClient.setStatus(Boolean.TRUE);
        vehicleClient.setIdUser(user);
        vehicleClient.setIdMark(markVehicle);
        session.save(vehicleClient);
        
        myvehicle.add(vehicleClient);
        
        
        
        
        
        
        tx.commit();
        out.println("Mapeamento realizado com sucesso");
        
        } catch(Exception e ) {
        out.println(e.getCause().toString());
        }*/
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
